/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditBooth.java
 *
 * Created on Mar 22, 2011, 2:00:06 PM
 */

package GUI;

import Business.BoothManage;
import Business.SpaceUnitMeasure;
import Entities.BoothInformation;
import java.sql.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class EditBooth extends javax.swing.JDialog {

    /** Creates new form EditBooth */
    public EditBooth(BoothInformation objbooth,java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        booth=objbooth;
        space=new SpaceUnitMeasure();
        objboothManager =new BoothManage();
        FillDataToComboSpaceUnitMeasure();
        FillDataToExhibitorID();
        txtBoothName.setText(booth.getName());
        txtSpace.setText(String.valueOf(booth.getSpace()));
        cbxSpaceMeasure.setSelectedItem(space.GetName(booth.getSpaceUnitMeasureID()));
        txtFees.setText(String.valueOf(booth.getFees()));
        cbxExhibitor.setSelectedItem(objboothManager.getExhibitorNameById(booth.getExhibitorID()));
        txtContractCode.setText(String.valueOf(booth.getContractCode()));
        if(booth.getStatus()==1)
          {
              cbStatus.setSelectedItem("Normal");
         }
       else
        {
            cbStatus.setSelectedItem("Block");
        }
        txtSponsor.setText(booth.getSponsor());

    }
    private void FillDataToComboSpaceUnitMeasure()
    {
        for(String spaceMeasure:space.getListToVector())
              cbxSpaceMeasure.addItem(spaceMeasure);
    }
   private void FillDataToExhibitorID()
    {
       for(String exhibitor:objboothManager.getExhibitorToArrayList())
           cbxExhibitor.addItem(exhibitor);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtBoothName = new javax.swing.JTextField();
        txtSponsor = new javax.swing.JTextField();
        txtSpace = new javax.swing.JTextField();
        txtFees = new javax.swing.JTextField();
        bntEdit = new javax.swing.JButton();
        bntReset = new javax.swing.JButton();
        bntCancel = new javax.swing.JButton();
        txtContractCode = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        cbxSpaceMeasure = new javax.swing.JComboBox();
        cbxExhibitor = new javax.swing.JComboBox();
        cbStatus = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel2.setText("Booth Name");

        jLabel5.setText("Space");

        jLabel6.setText("SpaceMeasure ");

        jLabel7.setText("Fees");

        jLabel8.setText("Contract Code");

        jLabel3.setText("Sponsors");

        jLabel4.setText("Exhibitor ");

        bntEdit.setText("Edit");
        bntEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntEditActionPerformed(evt);
            }
        });

        bntReset.setText("Reset");
        bntReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntResetActionPerformed(evt);
            }
        });

        bntCancel.setText("Cancel");
        bntCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelActionPerformed(evt);
            }
        });

        jLabel9.setText("Status");

        cbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Normal", "Block" }));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cbxSpaceMeasure, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(txtFees)
                                .addGap(1, 1, 1))
                            .addComponent(txtSpace)
                            .addComponent(txtBoothName, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(27, 27, 27)
                                .addComponent(txtSponsor, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(57, 57, 57)
                                .addComponent(cbStatus, 0, 118, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                                    .addComponent(bntReset))
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(cbxExhibitor, 0, 118, Short.MAX_VALUE)
                                            .addComponent(txtContractCode, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(29, 29, 29)
                                        .addComponent(bntCancel)))))
                        .addGap(62, 62, 62))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(112, 112, 112)
                        .addComponent(bntEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtBoothName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtSpace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(cbxSpaceMeasure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cbxExhibitor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(txtContractCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtFees, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtSponsor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bntEdit)
                    .addComponent(bntReset)
                    .addComponent(bntCancel))
                .addGap(70, 70, 70))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 491, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(27, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bntEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntEditActionPerformed
        // TODO add your handling code here:
      if(FormProcessing()){
                objboothManager=new BoothManage();
                BoothInformation  booth1=new BoothInformation();
                booth1.setName(txtBoothName.getText());
                booth1.setSpace(Integer.parseInt(txtSpace.getText()));
                booth1.setSpaceUnitMeasureID(space.GetID(cbxSpaceMeasure.getSelectedItem().toString()));
                booth1.setFees(Double.parseDouble(txtFees.getText()));

                String[] strID = cbxExhibitor.getSelectedItem().toString().split("-");
                //cut cbxExhibitor.getSelectedItem() to get exhibitorID
                booth1.setExhibitorID(Integer.parseInt(strID[0]));
                booth1.setContractCode(Integer.parseInt(txtContractCode.getText()));
                if(cbStatus.getSelectedItem().equals("Normal"))
                    booth1.setStatus(1);
                else
                   if(cbStatus.getSelectedItem().equals("Block"))
                   {
                       booth1.setStatus(0);
                   }

                booth1.setSponsor(txtSponsor.getText());
                Date todayD=new Date(System.currentTimeMillis());
                booth1.setMoDifiedDate(todayD);
                if(objboothManager.UpdateBooth(booth1,booth.getId())){
                     JOptionPane.showMessageDialog(rootPane, "Update Success!");
                     this.setVisible(false);
                  }
                else
                    JOptionPane.showMessageDialog(rootPane, "Update Error !");
           }
     
}//GEN-LAST:event_bntEditActionPerformed

    private void bntResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntResetActionPerformed
        // TODO add your handling code here:
        SetText();
}//GEN-LAST:event_bntResetActionPerformed

    private void SetText()
    {

       txtBoothName.setText("");
       txtSpace.setText("");
       txtFees.setText("");
       txtSponsor.setText("");
       txtContractCode.setText("");
    }
    private boolean isValidNumber(String number){
       String  expression="[0-9]{1,20}";
       CharSequence inputStr = number;
       Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
       Matcher matcher = pattern.matcher(inputStr);
       return matcher.matches();
   }
 private boolean FormProcessing()
 {
        if(txtBoothName.getText().length()==0)
        {
            JOptionPane.showMessageDialog(rootPane, "Booth Name field is not blank !");
            txtBoothName.requestFocusInWindow();
            return false;
        }
     else if(txtSpace.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(rootPane, "Booth space field is not blank !");
            txtSpace.requestFocusInWindow();
            return false;
        }
     else if(!isValidNumber(txtSpace.getText()))
        {
         JOptionPane.showMessageDialog(rootPane, "Booth space field is must numbers !");
            txtSpace.requestFocusInWindow();
            return false;
        }
     else if(txtFees.getText().length() == 0)
         {
            JOptionPane.showMessageDialog(rootPane, "Booth Fees field is not blank !");
            txtFees.requestFocusInWindow();
            return false;
        }
     else if (!isValidNumber(txtFees.getText()))
           {
             JOptionPane.showMessageDialog(rootPane, "Booth fees field is must numbers  !");
             txtFees.requestFocusInWindow();
             return false;
           }
       else if(txtSponsor.getText().length()==0)
         {
            JOptionPane.showMessageDialog(rootPane, "Booth sponsor field is not blank !");
            txtSponsor.requestFocusInWindow();
            return false;
        }
       else  if (txtContractCode.getText().length() == 0)
          {
            JOptionPane.showMessageDialog(rootPane, "ContractCode field is not blank !");
            txtContractCode.requestFocusInWindow();
            return false;
          }
       else if (!isValidNumber(txtContractCode.getText()))
         {
            JOptionPane.showMessageDialog(rootPane, "ContractCode field is must numbers  !");
            txtContractCode.requestFocusInWindow();
            return false;
         }
        return true;
 }
    private void bntCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_bntCancelActionPerformed

    /**
    * @param args the command line arguments
    */
  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntCancel;
    private javax.swing.JButton bntEdit;
    private javax.swing.JButton bntReset;
    private javax.swing.JComboBox cbStatus;
    private javax.swing.JComboBox cbxExhibitor;
    private javax.swing.JComboBox cbxSpaceMeasure;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtBoothName;
    private javax.swing.JTextField txtContractCode;
    private javax.swing.JTextField txtFees;
    private javax.swing.JTextField txtSpace;
    private javax.swing.JTextField txtSponsor;
    // End of variables declaration//GEN-END:variables
    BoothManage objboothManager;
    SpaceUnitMeasure space;
    //ExhibitorManage Exhibitor;
    BoothInformation booth;
}
